package com.leetone;

import java.util.concurrent.locks.ReentrantLock;

/**
* @ClassName: ReentrantLock
* @Description: 【显示锁的相关操作,这是第一种方式，两个方法之间的锁是独立的】
* @date 2018年8月29日 下午1:55:09
* @Company: 上海未泰信息技术有限公司
* @author:[谭磊][leetone123@163.com]
*/
public class ReentrantLockTest {
	public static void main(String[] args) {
		final Countx count = new Countx();
		for(int i=0;i<2;i++) {
			new Thread() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					super.run();
					count.get();
				}
			}.start();
		}
		
		for(int i=0;i<2;i++) {
			new Thread() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					super.run();
					count.put();
				}
			}.start();
		}
	}
	
}
class Countx{
	public void get() {
		final ReentrantLock lock = new ReentrantLock();
		try {
			//加锁
			lock.lock();
			System.out.println(Thread.currentThread().getName()+"\t get begin");
			//模仿干活
			Thread.sleep(1000L);
			System.out.println(Thread.currentThread().getName()+"\t get end");
			//解锁
			lock.unlock();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void put() {
		final ReentrantLock lock = new ReentrantLock();
		try {
			lock.lock();
			System.out.println(Thread.currentThread().getName()+"\t put begin");
			Thread.sleep(1000L);
			System.out.println(Thread.currentThread().getName()+"\t put end");
			lock.unlock();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
